<?php

/**
 *  碎片模块 "{{block}}".
 *
 * The followings are the available columns in table '{{block}}':
 * @property integer $zbl_id      编号
 * @property integer $zm_id       会员id
 * @property string $zbl_title    碎片名称
 * @property string $zbl_pos      碎片英文名称
 * @property integer $zbl_type    碎片类型1HTML2模板
 * @property string $zbl_data     碎片内容
 * @property string $zbl_template 碎片模版
 * @property integer $zna_id      所在页面
 * @property integer $inputtime   添加时间
 * @property integer $updatetime  更新时间
 */
class Block extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Block the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{block}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zm_id, zbl_type, zna_id, inputtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('zbl_title', 'length', 'max'=>50),
			array('zbl_pos', 'length', 'max'=>30),
			array('zbl_data, zbl_template', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zbl_id, zm_id, zbl_title, zbl_pos, zbl_type, zbl_data, zbl_template, zna_id, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zbl_id' => 'ID',
			'zm_id' => '会员ID',
			'zbl_title' => '碎片名',
			'zbl_pos' => '英文名',
			'zbl_type' => '类型',   //'碎片类型1HTML2模板'
			'zbl_data' => '内容',
			'zbl_template' => '模板',
			'zna_id' => '所在页面',
			'inputtime' => '添加时间',
			'updatetime' => '更新时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zbl_id',$this->zbl_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zbl_title',$this->zbl_title,true);
		$criteria->compare('zbl_pos',$this->zbl_pos,true);
		$criteria->compare('zbl_type',$this->zbl_type);
		$criteria->compare('zbl_data',$this->zbl_data,true);
		$criteria->compare('zbl_template',$this->zbl_template,true);
		$criteria->compare('zna_id',$this->zna_id);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);

		$criteria->order = " inputtime  DESC";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/*
	 *获取当前类型
	 * */
	public function getType($typeid)
	{
		if($typeid==1){
			$typename = 'HTML';
		}else{
			$typename = '模板';
		}
		return $typename;
	}
	/*
	 *获取当前会员售后模板
	 * */
	public function getSaleExpressByActiveUser(){
		$criteria = new CDbCriteria;
		$criteria->compare('zm_id',Company::model()->getCmpUserId());
		$criteria->compare('zbl_type','1');
		$criteria->compare('zbl_pos','service');
		$data = $this->findAll($criteria);
		return CHtml::listData($data,'zbl_id','zbl_title');
	}

	/*
	 *  获取指定碎片
	 *  @ $pos 碎片英文名
	 *  @ $type 类型 1HTMl 2模板
	 * */
	public function Getpos($pos = null ,$type = 1){
		if( $pos != null ){
			$data = $this->find( array(
					'select' => 'zbl_data',
					'condition' => 'zbl_type='.$type.' AND zbl_pos = \''.$pos.'\'',
				)
			);
		}else{
			$data = '加载内容出错';
		}
		return $data['zbl_data'];
	}
	
	/*
	 * 保存数据前自动处理
	 * */
	protected function beforeSave()
	{
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->inputtime = time();
				$this->updatetime = 0;
			}else{
				$this->updatetime = time();
			}
			return true;
		}else{
			return false;
		}
	}
}